/*
 * WTF Deferred Shading Buffer Pre-processor: Fragment Shader
 */

 #version 120

varying vec3 normal;
varying float depth;
varying vec3 specular;
varying float shininess;

void main() {
	//vec4 pos = gl_ModelViewProjectionMatrix * gl_FragCoord;
	//pos.z = pos.z *pos.w;

	//float fDepth = (1.f / gl_DepthRange.near - 1.f / depth) / (1.f / gl_DepthRange.near - 1.f / gl_DepthRange.far);
	//float fColor = gl_DepthRange.near; //fDepth;
    
    // perform depth-packing (allowed range: [-1.0, 1.0])
    // all these functions have crap range, but thanks to 32-bit/channel textures it works
        float fDepth;
        // a) inverse-square method (range 0 to 1)
        //fDepth = (1.0 / sqrt(depth * 2.0)) * 0.5 - 0.2;       
        fDepth = depth;

        // b) exponential method (range 0 to 1)
        // fDepth = exp(-depth / 0.2);  // not good
    // store colour data in buffer 1
	gl_FragData[0] = gl_Color;
    // store eyespace normals and depth in buffer 2
	gl_FragData[1] = vec4(normal.xyz, fDepth);
    gl_FragData[2] = vec4(specular, shininess);
}
